<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\MemberModel;
use Admin\Model\CityModel;
use Admin\Model\RegionModel;

/**
 * 联盟商户类,包含门店管理和工程师管理
 */
class UnionStoreController extends Controller
{
	// 该联盟商户的id
	private $member_id;
	function __construct(){
		parent::__construct();

		$noCheckAction = array('');
		if(!in_array(ACTION_NAME,$noCheckAction)) {
			if($_SESSION['member_id']){
				$this->member_id = $_SESSION['member_id'];
				if($_SESSION['m_type']==2 || $_SESSION['m_type']==1){

				}else{
					returnMobileJson(0,'未登录');
				}
				$this->Member = new MemberModel();
			}else{
				returnMobileJson(0,'未登录');
			}
		}


	}

	/**
	 * 判断是否登录
	 */
	public function checkLogin(){
		returnMobileJson(200,'已登录');
	}

	/**
	 * 获取门店列表
	 * @return array 门店列表信息
	 */
	public function get_store_list(){
		$union_store_list=$this->Member->get_member_list(0,null,6,$this->member_id);
		if(!$union_store_list)
			returnMobileJson(201,'没有数据');
		else
			returnMobileJson(200,'获取成功',$union_store_list);
	}

	/**
	 * 添加门店
	 * @param array $union_store 该联盟商户的信息
	 * @param array $city 该联盟商户的城市信息
	 */
	public function add_union_store(){
		$m_name = $_POST['m_name'];
		$m_account = $_POST['m_account'];
		$m_pwd = $_POST['m_pwd'];
		$re_id = $_POST['re_id'];

		if(empty($m_name) || empty($m_pwd) || empty($re_id)){
			returnMobileJson(201,'参数错误');
		}
		//验证账号是否为手机号码
		if(!preg_match("/^1\d{10}$/",$m_account)){
			returnMobileJson(202,'参数错误,账号需为手机号');
		}
		if($this->Member->get_exist_member($m_account)){
			returnMobileJson(203,'账号已存在');
		}
		// 获取该联盟商户的city_id

		$union_store=$this->Member->get_member($this->member_id);

		$data = array('m_name'=>$m_name,'m_status'=>0,'re_id'=>$re_id, 'm_account'=>$m_account, 'm_pwd'=> md5($m_pwd), 'm_date' => date('Y-m-d H:i:s') , 'pa_id'=>$this->member_id, 'm_type'=>'6', 'city_id'=>$union_store['city_id']);
		
		if($this->Member->set_member($data)){
			returnMobileJson(200,'添加成功');		
		}else{
			returnMobileJson(204,'添加失败');
		}	
	}

	/**
	 * 获取门店信息
	 * @return array 门店信息
	 * 订单总数没有
	 */
	public function get_union_store(){
		$member_id=$_POST['member_id'];
		if(empty($member_id))
			returnMobileJson(201,'参数错误');
		$union_store=$this->Member->get_member($member_id);
		if(!$union_store)
			returnMobileJson(202,'没有数据');
		else
			returnMobileJson(200,'获取成功',$union_store);
	}
	
	/**
	 * 修改门店信息
	 */
	public function edit_union_store(){
		$member_id=$_POST['member_id'];
		$m_name=$_POST['m_name'];
		$m_status=$_POST['m_status'];
//		$m_account=$_POST['m_account'];
		$m_pwd=$_POST['m_pwd'];
		$m_address=$_POST['m_address'];
		$re_id = $_POST['re_id'];
		$m_business_hours=$_POST['m_business_hours'];
		$m_tel = $_POST['m_tel'];

		if(empty($member_id)){
			returnMobileJson(201,'参数错误');
		}

//		if(!preg_match("/^1[3456789]{1}\d{9}$/",$m_account)){
//			returnMobileJson(202,'参数错误,账号需为手机号');
//		}
//		if($this->Member->get_exist_member($m_account)){
//			returnMobileJson(203,'账号已存在');
//		}

		$data = array('m_date' => date('Y-m-d H:i:s'));
		($m_status=='0' || $m_status=='1') && $data['m_status'] = $m_status;
		$m_pwd && $data['m_pwd'] = md5($m_pwd);
		$m_name && $data['m_name'] = $m_name;
		$m_address && $data['m_address'] = $m_address;
		$m_business_hours && $data['m_business_hours'] = $m_business_hours;
		$re_id && $data['re_id'] = $re_id;
		$m_tel && $data['m_tel'] = $m_tel;




		if($this->Member->set_member($data,$member_id))
			returnMobileJson(200,'编辑成功');
		else
			returnMobileJson(204,'添加失败');
	}

	/**
	 * 获取工程师列表
	 * @return array 工程师列表信息
	 */
	public function get_engineer_list(){
		$union_engineer_list=$this->Member->get_member_list(0,null,5,$this->member_id);
		if(!$union_engineer_list)
			returnMobileJson(201,'没有数据');
		else
			returnMobileJson(200,'获取成功',$union_engineer_list);
	}

	/**
	 * 添加工程师
	 */
	public function add_union_engineer(){
		$m_name=$_POST['m_name'];
		$m_account=$_POST['m_account'];
		$m_pwd=$_POST['m_pwd'];
		$re_id = $_POST['re_id'];

		if(empty($m_name) || empty($m_pwd)){
			returnMobileJson(201,'参数错误');
		}
		//验证账号是否为手机号码
		if(!preg_match("/^1[3456789]{1}\d{9}$/",$m_account)){
			returnMobileJson(202,'参数错误,账号需为手机号');
		}
		if($this->Member->get_exist_member($m_account)){
			returnMobileJson(203,'账号已存在');
		}
		if(!$re_id){
			returnMobileJson(202,'区域不能为空');
		}

		// 获取该门店的city_id
		$union_store=$this->Member->get_member($this->member_id);

		$data = array( 'm_account'=>$m_account, 'm_name'=>$m_name,'re_id'=>$re_id, 'm_pwd'=> md5($m_pwd), 'm_date' => date('Y-m-d H:i:s'), 'pa_id'=>$this->member_id, 'm_type'=>'5', 'city_id'=>$union_store['city_id']);

		if($this->Member->set_member($data)){
			returnMobileJson(200,'添加成功');
		}else{
			returnMobileJson(204,'添加失败');
		}	
	}

	/**
	 * 获取工程师信息
	 * @param array $union_store 该联盟商户的信息
	 * @param array $city 该联盟商户的城市信息
	 * @param  array $region 该该联盟商户的城市和合伙人的城市开放区域
	 * @return array $union_engineer 工程师信息
	 * 订单总数没有
	 */
	public function get_union_engineer(){
		$member_id = $_POST['member_id'];
		$union_engineer['basic_info']=$this->Member->get_member($member_id);

		// 获取该门店的合伙人
		$union_store=$this->Member->get_member($member_id);
		// 获取该合伙人的city
		$City = new CityModel();
		$city=$City->get_province_city($union_store['city_id']);
		$union_engineer['city']=$city['city'];
		// 获取该合伙人的开放区域region
		$Region = new RegionModel();
		$region=$Region->get_region($union_store['city_id']);
		$union_engineer['region']=$region;
		if(!$union_engineer)
			returnMobileJson(201,'没有数据');
		else
			returnMobileJson(200,'获取成功',$union_engineer);
	}

	/**
	 * 修改工程师信息
	 * @param array $data 工程师详细信息
	 */
	public function edit_union_engineer(){
		$member_id=$_POST['member_id'];
		$m_name=$_POST['m_name'];
		$m_status=$_POST['m_status'];
		$m_pwd=$_POST['m_pwd'];
		$re_id=$_POST['re_id'];
		$m_job_num=$_POST['m_job_num'];
		$m_head_imgurl=$_POST['m_head_imgurl'];

		if( empty($member_id)){
			returnMobileJson(201,'参数错误');
		}

		$member_id=$_POST['member_id'];


		if(empty($member_id)){
			returnMobileJson(201,'参数错误');
		}
		$data=array();
		$re_id && $data['re_id'] = $re_id;
		$m_pwd && $data['m_pwd'] = md5($m_pwd);
		($m_status=='1' || $m_status=='0') && $data['m_status'] = $m_status;
		$m_name && $data['m_name'] = $m_name;
		$m_job_num && $data['m_job_num'] = $m_job_num;
		$m_head_imgurl && $data['m_head_imgurl'] = $m_head_imgurl;
		
		if($this->Member->set_member($data,$member_id))
			returnMobileJson(200,'修改成功');
		else
			returnMobileJson(204,'添加失败');
	}

	/**
	 * 当前登录账号所在城市的所有区域
	 */
	public function get_area(){
		$union_store=$this->Member->get_member($this->member_id);
		// 获取该合伙人的区域
		$Region = new RegionModel();
		$region=$Region->get_region($union_store['city_id']);
		returnMobileJson(200,'数据获取成功',$region);
	}


	
}